//
//  XGSettingTableViewController.m
//  BSBuDeJie
//
//  Created by monkey on 2018/12/16.
//  Copyright © 2018 itcast. All rights reserved.
//

#import <SVProgressHUD/SVProgressHUD.h>

#import "XGSettingTableViewController.h"

#import "NSFileManager+Extension.h"

@interface XGSettingTableViewController ()

@end

@implementation XGSettingTableViewController

#pragma mark - 控制器生命周期方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"设置";
}

#pragma mark - 其他方法

- (void)gotoAppStore
{
    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/bai-si-bu-jie-zui-da-gao-xiao/id1095183888?ign-mpt=uo%253D2&mt=12"];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
         [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }
}

- (void)callTelephone
{
    NSURL *url = [NSURL URLWithString:@"tel://17300520041"];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }
}

- (void)sendMessage
{
    NSURL *url = [NSURL URLWithString:@"sms://17300520041"];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }
}

// 清除缓存
- (void)clearCache
{
   
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    [NSFileManager getFileSize:cachePath completion:^(NSUInteger fileSize) {
        if (fileSize == -1) {
            DEBUG_Log(@"文件路径错误!");
            return;
        } else if (fileSize == 0) {
            return;
        } else {
            NSString *str = nil;
            if (fileSize > 1000 * 1000) {
                str = [NSString stringWithFormat:@"%.2f MB",fileSize / 1000.f / 1000.f];
            } else if (fileSize > 1000) {
                str = [NSString stringWithFormat:@"%.2f KB",fileSize / 1000.f];
            } else {
                str = [NSString stringWithFormat:@"%zd B",fileSize];
            }
            
            [NSFileManager removeFilePath:cachePath completion:^(NSError * _Nullable error) {
                if (error) {
                    DEBUG_Log("清理缓存失败");
                    return;
                } else {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"共清除%@缓存",str]];
                        NSLog(@"%@",cachePath);
                    });
                }
            }];
        }
    }];
}

@end
